﻿using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System;
using System.Management;

namespace SimpleInfrastructure.Helpers
{
    public static class EnvHelper
    {
        public static string GetClientID()
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Session.SessionID;
            }
            return string.Empty;
        }

        public static string GetPhysicalPath(string path)
        {
            if (path.StartsWith("~") && HostingEnvironment.IsHosted)
            {
                return HostingEnvironment.MapPath(path);
            }
            return path;
        }
        public static HttpContext CurrentContext()
        {
            var httpContext = HttpContext.Current;
            if (httpContext == null)
            {
                return null;
            }

            try
            {
                // The "Request" property throws at application startup on IIS integrated pipeline mode.
                if (httpContext.Request == null)
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
            return httpContext;
        }

        public static int GetCpuCoreCount()
        {
            try
            {
                //获取CPU序列号代码
                int cpuInfo = 1;//cpu序列号
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (var mo in moc)
                {
                    cpuInfo = int.Parse(mo.Properties["NumberOfCores"].Value.ToString());
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch(Exception ex)
            {
                return 1;
            }
            finally
            {
            }
        }
    }
    
}
